package view.userinteface;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;

import view.controller.ControllerPrincipal;
import beans.BeanProveedor;

public class JInternalFrameModificarProveedor extends javax.swing.JInternalFrame {

	private static final long serialVersionUID = 1L;

	private JTextField txtTelefono;
	private JTextField txtDomicilio;
	private JTextField txtCUIT;
	private JTextField txtRazonSocial;
	private JButton btnTraerProveedor;
	private JLabel lblTelefono;
	private JLabel lblDNI;
	private JLabel lblCUIT;
	private JLabel lblNombre;
	private JButton jButton3;
	private JButton jButton2;
	private static JInternalFrameModificarProveedor instancia;
	
	public static JInternalFrameModificarProveedor getInstancia(){
		if(instancia == null)
			instancia = new JInternalFrameModificarProveedor();
		return instancia;
	}
		
	public JInternalFrameModificarProveedor() {
		super();
		initGUI();
	}
	
	private void initGUI() {
		try {
			this.setPreferredSize(new java.awt.Dimension(407, 268));
			this.setBounds(0, 0, 407, 268);
			setVisible(true);
			getContentPane().setLayout(null);
			this.setTitle("Modificar proveedor");
			{
				lblNombre = new JLabel();
				getContentPane().add(lblNombre);
				lblNombre.setText("Razon social: ");
				lblNombre.setBounds(31, 89, 177, 14);
			}
			{
				lblCUIT = new JLabel();
				getContentPane().add(lblCUIT);
				lblCUIT.setText("CUIT: ");
				lblCUIT.setBounds(73, 15, 73, 14);
			}
			{
				lblDNI = new JLabel();
				getContentPane().add(lblDNI);
				lblDNI.setText("Domicilio:");
				lblDNI.setBounds(31, 122, 177, 14);
			}
			{
				lblTelefono = new JLabel();
				getContentPane().add(lblTelefono);
				lblTelefono.setText("Telefono: ");
				lblTelefono.setBounds(31, 155, 177, 14);
			}
			{
				txtRazonSocial = new JTextField();
				getContentPane().add(txtRazonSocial);
				txtRazonSocial.setBounds(220, 89, 160, 21);
			}
			{
				txtCUIT = new JTextField();
				getContentPane().add(txtCUIT);
				txtCUIT.setText("Ingrese CUIT de proveedor a modificar");
				txtCUIT.setBounds(149, 12, 244, 21);
			}
			{
				txtDomicilio = new JTextField();
				getContentPane().add(txtDomicilio);
				txtDomicilio.setBounds(220, 122, 160, 21);
			}
			{
				txtTelefono = new JTextField();
				getContentPane().add(txtTelefono);
				txtTelefono.setBounds(220, 155, 160, 21);
			}
			{
				jButton2 = new JButton();
				getContentPane().add(jButton2);
				jButton2.setText("Aceptar");
				jButton2.setBounds(73, 196, 97, 27);
				jButton2.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent evt) {
						if(validar()){
							BeanProveedor beanProveedor = new BeanProveedor();
							beanProveedor.setCuit(txtCUIT.getText());
							beanProveedor.setDireccion(txtDomicilio.getText());
							beanProveedor.setRazonSocial(txtRazonSocial.getText());
							beanProveedor.setTelefono(txtTelefono.getText());
							if(ControllerPrincipal.getInstancia().modificarProveedor(beanProveedor)){
								JOptionPane.showMessageDialog(JInternalFrameModificarProveedor.getInstancia(),"Proveedor modificado correctamente");
								dispose();
							}else{
								JOptionPane.showMessageDialog(JInternalFrameModificarProveedor.getInstancia(),"No se pudo modificar el proveedor");
							}
							
						}else{
							JOptionPane.showMessageDialog(JInternalFrameModificarProveedor.getInstancia(),"Todos los campos deben estar completos");
						}
					}

				});
			}
			{
				jButton3 = new JButton();
				getContentPane().add(jButton3);
				jButton3.setText("Cancelar");
				jButton3.setBounds(215, 196, 100, 27);
				jButton3.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent evt) {
						dispose();
					}
				});
			}
			{
				btnTraerProveedor = new JButton();
				getContentPane().add(btnTraerProveedor);
				btnTraerProveedor.setText("Cargar Proveedor");
				btnTraerProveedor.setBounds(121, 45, 159, 26);
				btnTraerProveedor.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent evt) {
						BeanProveedor beanProveedor = ControllerPrincipal.getInstancia().buscarProveedor(txtCUIT.getText());
						if(beanProveedor.getCuit()!=null && !"".equalsIgnoreCase(beanProveedor.getCuit())){
							txtDomicilio.setText(beanProveedor.getDireccion());
							txtRazonSocial.setText(beanProveedor.getRazonSocial());
							txtTelefono.setText(beanProveedor.getTelefono());
							txtCUIT.setEditable(false);
						}
						else{
							JOptionPane.showMessageDialog(JInternalFrameModificarProveedor.getInstancia(),"Proveedor Inexistente");
						}
					}
				});
			}

			pack();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	private boolean validar() {
		if("".equalsIgnoreCase(txtCUIT.getText()) || "".equalsIgnoreCase(txtDomicilio.getText()) || "".equalsIgnoreCase(txtRazonSocial.getText()) 
				|| "".equalsIgnoreCase(txtTelefono.getText()))
			return false;
		else
			return true;
	}
	

}
